//
//  RGSearchViewController.m
//  RedGrass
//
//  Created by wukong on 2019/2/13.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGSearchViewController.h"
#import "RGSearchView.h"
#import "RGSearchUsersListViewController.h"
#import "RGSeachVedioListViewController.h"

@interface RGSearchViewController ()<RGSearchViewDelegate>

@property (strong, nonatomic) RGSearchView *searchView;
@property (strong, nonatomic) RGSeachVedioListViewController *vedioListVC;
@property (strong, nonatomic) RGSearchUsersListViewController *userListVC;
@property (assign, nonatomic) BOOL isfirstLoad;
@property (strong, nonatomic) UIViewController *currentVc;


@end

@implementation RGSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _isfirstLoad = YES;
    self.customNavBar.hidden = YES;
    self.NavStyleType = RGCustomNavStyleType_default;
    [self.customContentView addSubview:self.searchView];
    [_searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if(_isfirstLoad){
        _searchView.containerView.contentSize = CGSizeMake(KScreenWidth * 2, KScreenHeight  - SafeAreaTopHeight - SafeAreaBottomHeight - 50);
        self.userListVC.view.frame = CGRectMake(KScreenWidth, 0, KScreenWidth, KScreenHeight  - SafeAreaTopHeight - SafeAreaBottomHeight - 50);
        [self addChildViewController:self.userListVC];
        [_searchView.containerView addSubview:_userListVC.view];

        self.vedioListVC.view.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight  - SafeAreaTopHeight - SafeAreaBottomHeight - 50);;
        [self addChildViewController:self.vedioListVC];
        [_searchView.containerView addSubview:_vedioListVC.view];
        _currentVc = _vedioListVC;
        _isfirstLoad = NO;
        [_searchView becomFirstReponse];
    }
}

#pragma mark - viewDeleagte
-(void) changeSearchResultUIWithView:(RGSearchView *)view tag:(NSInteger)tag{
     AA_WEAK_MAKE(self) weakSelf = self;
    if(tag == 0){

        if(![_currentVc isKindOfClass:[RGSeachVedioListViewController class]]){
            [UIView animateWithDuration:0.3 animations:^{
                self.searchView.containerView.contentOffset = CGPointMake(0, 0);
            } completion:^(BOOL finished) {
                if(finished){
                    weakSelf.currentVc = weakSelf.vedioListVC;
                }
            }];
        }
    }else if (tag == 1){
        if(![_currentVc isKindOfClass:[RGSearchUsersListViewController class]]){
            [UIView animateWithDuration:0.3 animations:^{
                self.searchView.containerView.contentOffset = CGPointMake(KScreenWidth, 0);
            } completion:^(BOOL finished) {
                if(finished){
                    weakSelf.currentVc = weakSelf.userListVC;
                }
            }];
        }
    }
}

-(void)backWithView:(RGSearchView *)view{
    [self.navigationController popViewControllerAnimated:YES];
}

//搜索
-(void) searchVedioAndUserWithView:(RGSearchView *)view text:(NSString *)text{
    if([text length] == 0){
        [self showTextMessageWithMessage:@"搜索关键词不能为空"];
        return;
    }
    [_userListVC searchVedioWithContent:text isLoadMore:NO];
    [_vedioListVC searchVedioWithContent:text isLoadMore:NO];
}

#pragma mark - getter

-(RGSearchView *)searchView{
    if(!_searchView){
        _searchView = [[RGSearchView alloc]init];
        _searchView.viewDelegate = self;
    }
    return _searchView;
}

-(RGSearchUsersListViewController *)userListVC{
    if(!_userListVC){
        _userListVC = [[RGSearchUsersListViewController alloc]init];
    }
    return _userListVC;
}

-(RGSeachVedioListViewController *)vedioListVC{
    if(!_vedioListVC){
        _vedioListVC = [[RGSeachVedioListViewController alloc]init];
    }
    return _vedioListVC;
}


@end
